//
//  JXPMapViewController.m
//  模拟-百思不得姐
//
//  Created by ColinJ on 16/8/16.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "JXPMapViewController.h"


@interface JXPMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>

@property (nonatomic,strong)BMKMapView* mapView;
@property (nonatomic,strong)BMKLocationService *locService;
@property (nonatomic,assign)BOOL locState;


@end

@implementation JXPMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == 1) {
        UIBarButtonItem *sendBtn = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:(UIBarButtonItemStylePlain) target:self action:@selector(sendMyLoc)];
        self.navigationItem.rightBarButtonItem = sendBtn;
        UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemCancel) target:self action:@selector(backClick)];
        self.navigationItem.leftBarButtonItem = backBtn;
    }
    self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64)];
    [self.view addSubview: self.mapView];
//    [self btn];
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0))
    {
        self.navigationController.navigationBar.translucent = NO;
    }
    _locService = [[BMKLocationService alloc]init];     //定位功能初始化 必须写在didload里面
    _locState = YES;
    [_locService startUserLocationService];     //启动定位服务
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
}

- (void)backClick{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)sendMyLoc{
    self.sendLoc(_myLoc);
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)printLoaction{
    NSLog(@")))))))))))))))))))))%f,%f",_locService.userLocation.location.coordinate.latitude,_locService.userLocation.location.coordinate.longitude);
    
}

- (void)btn{
    UIButton *changBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    changBtn.frame = CGRectMake(0, 50, 40, 40);
    changBtn.backgroundColor = [UIColor whiteColor];
    [changBtn setTitle:@"定位" forState:(UIControlStateNormal)];
    [changBtn setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [changBtn addTarget:self action:@selector(mapTypeChange) forControlEvents:(UIControlEventTouchUpInside)];
    [_mapView addSubview:changBtn];
}

- (void)mapTypeChange {
    
    if (_locState == YES) {
        NSLog(@"进入普通定位态");
        [_locService startUserLocationService];     //启动定位服务
        _mapView.showsUserLocation = NO;//先关闭显示的定位图层
        _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
        _mapView.showsUserLocation = YES;//显示定位图层
        
    }else{
        NSLog(@"暂停定位");
        [_locService stopUserLocationService];
        _mapView.showsUserLocation = NO;
    }
    _locState = !_locState;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(printLoaction) name:@"getLocation" object:nil];
}

//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [_mapView updateLocationData:userLocation];
    self.myLoc = [userLocation.location coordinate];
    NSLog(@"---------heading is %@",userLocation.heading);
}

    



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated
{
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    self.locService.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    self.locService.delegate = nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
